$(document).ready(function () {
    var currPage = 1;
    var pageInfo;
    showPage(currPage);

    function showPage(currPage) {
        $.ajax({
            url: 'goods/getAllByPageJson',//提交的地址
            data: {'pageSize': 5, 'currPage': currPage},//提交参数
            dataType: 'json',//服务器返回值的类型
            success: function (dto) {
                console.log(dto);
                pageInfo = dto.obj;
                var $adGoodsli = $('.adGoodsUl > li:first');
                $adGoodsli.css('display', 'block');
                $('.adGoodsUl > li:gt(0)').remove();//gt 大于第一个的元素都删除掉
                var goodses = dto.tList;
                $(goodses).each(function (i, goods) {
                    var $new$adGoodsli = $adGoodsli.clone();//克隆复制一份
                    $new$adGoodsli.find('.adGoodsId').text(goods.gid);
                    $new$adGoodsli.find('.adGoodsImg > img').attr('src', "upload/goods/" + goods.gimg);
                    $new$adGoodsli.find('.adGoodsName').text(goods.gname);
                    $new$adGoodsli.find('.adGoodsPrice').text(goods.gprice);
                    $new$adGoodsli.find('.adGoodsNum').text(goods.gnum);
                    //循环出新的广告商品后，将循环出的内容追加到ul的内部
                    $('.adGoodsUl').append($new$adGoodsli);

                });
                $(".adGoodsCar > input").click(function () {

                    var $adGoodsInfo = $(this).parent().parent();
                    var  gid = $adGoodsInfo.children('div').eq(0).text();
                    var  gname = $adGoodsInfo.children('div').eq(2).text();
                    var  gprice = $adGoodsInfo.children('div').eq(3).text();
                    var  gnum = $adGoodsInfo.children('div').eq(4).text();

                    //alert(gid + ' '+ gname + ' '+gprice + ' '+gnum);
                    var $goodsCarUl = $('.goodsCar > ul');//得到购物车对象
                    var $newli = $goodsCarUl.children('li:eq(0)').clone();//复制第一行
                    $newli.children('span:eq(0)').children('input:checkbox').addClass("orderinfoId");
                    $newli.children('span:eq(0)').children('input:checkbox').val(gid);


                    $newli.children('span:eq(1)').text(gid);
                    $newli.children('span:eq(2)').text(gname);
                    $newli.children('span:eq(3)').text(gprice);
                    $newli.children('span:eq(4)').text(1);
                    $newli.children('span:eq(5)').text(1*gprice);
                    $newli.children('span:eq(6)').html("<input type='button' value='删除'>");
                    $goodsCarUl.append($newli);
                    $goodsCarUl.find("li > span > input:button").click(function(){
                        $(this).parent().parent().remove();
                    });
                    $goodsCarUl.find("li > span > input:checkbox").click(function(){
                        //alert('dd');
                    });

                });
                $adGoodsli.css('display', 'none');
            }//回调函数  当服务器有返回的时候调用的函数
        });
    }

    $('.pageSpans > span').click(function () {
        var alt = $(this).attr('alt');
        var flag = false;
        switch (alt) {
            case 'first':
                if (pageInfo.currPage > 1) {
                    currPage = 1;
                } else {
                    flag = true;
                }
                break;
            case 'prev':
                if (pageInfo.currPage > 1) {
                    currPage = currPage - 1;
                } else {
                    flag = true;
                }
                break;
            case 'next':
                if (pageInfo.currPage < pageInfo.pageCount) {
                    currPage = currPage + 1;
                } else {
                    flag = true;
                }
                break;
            case 'end':
                if (pageInfo.currPage < pageInfo.pageCount) {
                    currPage = pageInfo.pageCount;
                } else {
                    flag = true;
                }
                break;
            default:
                break;
        }
        if (!flag) {
            showPage(currPage);
        }
    });


});